package zabbix

import (
	"encoding/json"
	"errors"
)

func TriggerCreateData() (string,map[string]interface{}) {
	method := "trigger.create"
	var data map[string]interface{}
	jsonstr := []byte(`[
        {
            "description": "Processor load is too high on {HOST.NAME}",
            "expression": "{Linux server:system.cpu.load[percpu,avg1].last()}>5",
            "dependencies": [
                {
                    "triggerid": "17367"
                }
            ]
        },
        {
            "description": "Service status",
            "expression": "{Linux server:log[/var/log/system,Service .* has stopped].strlen()}<>0",
            "dependencies": [
                {
                    "triggerid": "17368"
                }
            ],
            "tags": [
                {
                    "tag": "service",
                    "value": "{{ITEM.VALUE}.regsub(\"Service (.*) has stopped\", \"\\1\")}"
                },
                {
                    "tag": "error",
                    "value": ""
                }
            ]
        }
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerDeleteData() (string,map[string]interface{}) {
	method := "trigger.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "12002",
        "12003"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerDeletedependenciesData() (string,map[string]interface{}) {
	method := "trigger.deletedependencies"
	var data map[string]interface{}
	jsonstr := []byte(`{
        {
            "triggerid": "14544"
        },
        {
            "triggerid": "14545"
        }
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerUpdateData() (string,map[string]interface{}) {
	method := "trigger.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "triggerid": "16040",
        "status": 1
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerAdddependenciesData() (string,map[string]interface{}) {
	method := "trigger.adddependencies"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "triggerid": "14092",
        "dependsOnTriggerid": "13565"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerGetData() (string,map[string]interface{}) {
	method := "trigger.Get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "triggerids": "16040",
        "output": "extend",
        "selectFunctions": "extend"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func TriggerDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func TriggerDeletedependencies(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerDeletedependenciesData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func TriggerUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func TriggerAdddependencies(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerAdddependenciesData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func TriggerGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


